package com.music.note_android.data.model

import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import java.util.UUID

/**
 * 笔记数据模型
 * 对应需求文档中的笔记实体类定义
 */
@Entity(tableName = "notes")
data class Note(
    @PrimaryKey
    val id: String = UUID.randomUUID().toString(),
    
    @ColumnInfo(name = "title")
    val title: String,
    
    @ColumnInfo(name = "created_time")
    val createdTime: Long,
    
    @ColumnInfo(name = "modified_time")
    val modifiedTime: Long,
    
    @ColumnInfo(name = "folder_id")
    val folderId: String? = null,
    
    @ColumnInfo(name = "thumbnail_path")
    val thumbnailPath: String? = null,
    
    @ColumnInfo(name = "data_path")
    val dataPath: String
) {
    /**
     * 获取格式化的创建时间
     */
    fun getFormattedCreatedTime(): String {
        return formatTime(createdTime)
    }
    
    /**
     * 获取格式化的修改时间
     */
    fun getFormattedModifiedTime(): String {
        return formatTime(modifiedTime)
    }
    
    /**
     * 时间格式化辅助方法
     */
    private fun formatTime(timestamp: Long): String {
        val now = System.currentTimeMillis()
        val diff = now - timestamp
        
        return when {
            diff < 24 * 60 * 60 * 1000 -> { // 今天
                val calendar = java.util.Calendar.getInstance()
                calendar.timeInMillis = timestamp
                String.format("%02d:%02d", calendar.get(java.util.Calendar.HOUR_OF_DAY), calendar.get(java.util.Calendar.MINUTE))
            }
            diff < 2 * 24 * 60 * 60 * 1000 -> { // 昨天
                "昨天"
            }
            else -> { // 更早
                val calendar = java.util.Calendar.getInstance()
                calendar.timeInMillis = timestamp
                String.format("%d月%d日", calendar.get(java.util.Calendar.MONTH) + 1, calendar.get(java.util.Calendar.DAY_OF_MONTH))
            }
        }
    }
    
    /**
     * 检查是否有缩略图
     */
    fun hasThumbnail(): Boolean {
        return !thumbnailPath.isNullOrEmpty()
    }
    
    /**
     * 获取显示标题（如果标题为空则返回默认标题）
     */
    fun getDisplayTitle(): String {
        return if (title.isBlank()) {
            val calendar = java.util.Calendar.getInstance()
            calendar.timeInMillis = createdTime
            "笔记 ${calendar.get(java.util.Calendar.YEAR)}-${String.format("%02d", calendar.get(java.util.Calendar.MONTH) + 1)}-${String.format("%02d", calendar.get(java.util.Calendar.DAY_OF_MONTH))}"
        } else {
            title
        }
    }
}